import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;

import javax.swing.*;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.filter.*;
import java.util.List;
import java.util.Iterator;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class SavePlan extends JDialog implements ActionListener{
	private JButton jbtOk,jbtAnnuler;
	private JLabel jlbSave;
	private JTextField jtfSave;
	private JPanel jpnMaster,jpnButton,jpnSave;
	static SAXBuilder sxb = new SAXBuilder();
	static Element racine = new Element("Reseaux");
	
	
	public SavePlan ()
	{
		jlbSave= new JLabel("  Donner le nom du fichier de Sauvgarde : ");
		jtfSave=new JTextField(20);
		jpnSave = new JPanel(new GridLayout(2,1));
		jpnMaster=new JPanel(new FlowLayout(FlowLayout.CENTER));
		jpnSave.add(jlbSave);
		jpnMaster.add(jtfSave);
		jpnSave.add(jpnMaster);
		
		jbtOk= new JButton("OK");
		jbtAnnuler= new JButton("Annuler");
		jpnButton=new JPanel(new FlowLayout(FlowLayout.CENTER));
		jpnButton.add(jbtOk);
		jpnButton.add(jbtAnnuler);
		
		getContentPane().add(jpnSave,BorderLayout.CENTER);
		getContentPane().add(jpnButton,BorderLayout.SOUTH);
		
		setSize(300,200);
		setVisible(true);
		setTitle("Sauvegarder le Plan");
		setResizable(false);
		setLocationRelativeTo(this.getParent());
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e)
			{
				Window AproposDlg = e.getWindow();
				AproposDlg.dispose();
			}
		}
		);
		
		jbtOk.addActionListener(this);
		
		
	}
	public void actionPerformed (ActionEvent e)
	{
		if(e.getSource() == jbtOk)	{
			enregistre(jtfSave.getText());
			this.dispose();
		}
		else if(e.getSource() == jbtAnnuler)	{	
			this.dispose();	
		}
	}
	
	static void enregistre(String fichier)
	   {
		System.out.println("Saving ...");
	      try
	      {
	    	 org.jdom.Document document = sxb.build(new File("Reseaux.xml"));
	         XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
	         sortie.output(document, new FileOutputStream(fichier+".xml"));
	      }
	      catch (java.io.IOException e){}
	      catch (Exception e){}
	   }

	
}